package com.jie.other.jetpack.repository

import com.jie.other.bean.ui.ViewModelItemBean
import kotlinx.coroutines.*
import kotlin.random.Random

object UserListRepository {

    /**
     * 默认从服务器获取用户列表
     */
    fun loadUserList(success : (List<ViewModelItemBean>) -> Unit, fail : (String) -> Unit) {
        MainScope().launch(Dispatchers.IO) {
            async {
                delay(2000)
                if (Random.nextBoolean()) {
                    val userList = ArrayList<ViewModelItemBean>()
                    userList.apply {
                        for (i in 0..99) {
                            add(ViewModelItemBean("name:$i", i == 0))
                        }
                    }
                    success.invoke(userList)
                } else {
                    fail.invoke("获取用户列表失败")
                }
            }
        }
    }

}